﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FastShip88.Admin
{
    public partial class AdmParcelSplit : System.Web.UI.Page
    {
        DBAccess.UsersDataContext db;
        string mCurCommandName;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBAccess.UsersDataContext();

            lblError.Visible = false;
            lblSuccess.Visible = false;
            mCurCommandName = "";

            if (!IsPostBack)
            {
                init_data();
                div_results.Visible = true;
                div_sub.Visible = false;
                div_selected.Visible = false;

            }

        }

        protected void fvSelected_PreRender(object sender, EventArgs e)
        {
     
        }

        protected void init_data()
        {

            string strParcelNo = "";

            var parcels = from p in db.ParcelInfos
                          where p.ParcelNo.Contains(strParcelNo) && ( p.StatusId == 40 /*请求分拆*/)
                          orderby p.UserId
                          select new { p.Id, p.UserId, p.ParcelNo, p.ShopId, ShopName = p.ShopInfo.name, p.Description, p.Weight, p.ArriveDate, UserName = (p.UserInfo.LastName  + p.UserInfo.FirstName ) };

            gvResults.DataSourceID = null;
            gvResults.DataSource = parcels;
            gvResults.DataBind();

        }

        protected void gvResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parcelId = SafeConvert.ToInt(gvResults.SelectedValue);

            div_sub.Visible = true;
            div_selected.Visible = true;

            gvSub.DataBind();
            fvSelected.ChangeMode(FormViewMode.Edit);
        }



        protected void fvSelected_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            
            if (e.CommandName == "Update")
            {
                switch (e.CommandArgument.ToString())
                {
                    case "SingleComplete":
                    case "SplitCancel":
                    case "AllComplete":
                        mCurCommandName = e.CommandArgument.ToString();
                        break;
                    default:
                        mCurCommandName = "Update";
                        break;
                }
            }

        }

        protected void gvSub_SelectedIndexChanged(object sender, EventArgs e)
        {
            fvSelected.ChangeMode(FormViewMode.Edit);
        }



        protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvResults.PageIndex = e.NewPageIndex;
            init_data();
        }

        protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {
            gvSub.DataBind();
            lblSuccess.Visible = true;

            int pid = SafeConvert.ToInt(gvResults.SelectedValue);
            var q_parcel = (from q in db.ParcelInfos
                            where q.Id == pid
                            select q).First();

            if (mCurCommandName == "SplitCancel")
            {
                Utility.UpdateParcelStatus(SafeConvert.ToInt(gvResults.SelectedValue), 65); //分拆取消
                Utility.SendEmail(Utility.EmailTypes.ParcelSplitFail, q_parcel.UserId.Value, q_parcel.ParcelNo);
            }

            if (mCurCommandName == "AllComplete")
            {
                Utility.UpdateParcelStatus(SafeConvert.ToInt(gvResults.SelectedValue), 60); //分拆完成
                Utility.SendEmail(Utility.EmailTypes.ParcelSplitComplete, q_parcel.UserId.Value, q_parcel.ParcelNo );

                //int userId = q_parcel.UserId.Value;
                //decimal unitPrice4 = Utility.GetUnitPriceByFeeType(4, userId); //分拆服务收费
                 
                //var q_bill = new DBAccess.UserBill();
                //q_bill.UserId = userId;
                //q_bill.FeeTypeId = 4; //分拆服务
                //q_bill.Date = DateTime.Now;
                //q_bill.IsCancelled = false;
                //q_bill.IsCleared = false;
                //q_bill.Quantity =q_parcel.Weight.Value;
                //q_bill.Amount = (decimal)q_bill.Quantity.Value * unitPrice4;
                //q_bill.ParcelId = pid;

                //db.UserBills.InsertOnSubmit(q_bill);
                //db.SubmitChanges();


            }


        }

        protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {

            int userId = Utility.GetUserId(this);

            switch (mCurCommandName)
            {
                case "SingleComplete":
                case "AllComplete":
                    e.Command.Parameters["@FinishDate"].Value = DateTime.Now;
                    e.Command.Parameters["@IsCancel"].Value = false;
                    e.Command.Parameters["@FinishUserId"].Value = userId;                    
                    break;
                case "SplitCancel":
                    e.Command.Parameters["@FinishDate"].Value = DateTime.Now;
                    e.Command.Parameters["@IsCancel"].Value = true;
                    e.Command.Parameters["@FinishUserId"].Value = userId;
                    break;
            }

        }

        protected void btnSplitAdd_Click(object sender, EventArgs e)
        {
            DBAccess.SubParcel p_sub = new FastShip88.DBAccess.SubParcel();
            p_sub.FromId = SafeConvert.ToInt(gvResults.SelectedValue);
            p_sub.CreateDate = DateTime.Now;
            p_sub.IsCancel = false;
            p_sub.Memo = "管理员后台添加的。";

            db.SubParcels.InsertOnSubmit(p_sub);
            db.SubmitChanges();
            gvResults_SelectedIndexChanged(null, null);
        }

    }
}
